package oop;

import java.util.Objects;

/**
 * equals() 定义：“对象内容相等”的逻辑，比如id相同，就是同一个人
 * equals()默认是比较两个对象的hashcode,但， 可以重写equals
 */
public class TestEquals {
    int id;
    String name;
    String pwd;

    TestEquals(int id,String name,String pwd){
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }
    /*@Override
    public String toString() {
        return "身份号"+id+"账户名："+name+"密码："+pwd;
    }*/  // 这里重写了，不显示地址

    public static void main(String[] args) {
        TestEquals t3 = new TestEquals(101,"zhan","123456");
        TestEquals t4 = new TestEquals(101,"li","654321");
        System.out.println(t3.toString());
        System.out.println(t4.toString());
        System.out.println(t3.equals(t4));

    }
    //重写equals  单击右键Generate 选equals
    //id相同就是相等
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TestEquals that = (TestEquals) o;
        return id == that.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

